package com.rssreader.client.mvc.views;

import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.custom.Portal;
import com.extjs.gxt.ui.client.widget.custom.Portlet;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.RootPanel;
import com.rssreader.client.RSSReaderConstants;
import com.rssreader.client.mvc.controllers.PortalController;
import com.rssreader.client.mvc.events.AppEvents;

public class PortalView extends View {

    private final Portal portal = new Portal(2);

    public PortalView(PortalController controller) {
	super(controller);
    }

    @Override
    protected void handleEvent(AppEvent event) {
	EventType eventType = event.getType();
	if (eventType.equals(AppEvents.NewPortletCreated)) {
	    onNewPortletCreated(event);
	}
    }

    @Override
    protected void initialize() {
	super.initialize();
	portal.setColumnWidth(0, 0.3);
	portal.setColumnWidth(1, 0.7);

	final Viewport viewport = new Viewport();
	viewport.setLayout(new FitLayout());
	viewport.add(portal);
	RootPanel.get().add(viewport);
    }

    private void onNewPortletCreated(AppEvent event) {
	final Portlet portlet = (Portlet) event.getData();
	// 將NavPortlet家在第一行，其餘的Portlet加到第二行
	if (portlet.getId() == RSSReaderConstants.NAV_PORTLET
		|| portlet.getId() == RSSReaderConstants.CHART_PORTLET) {
	    portal.add(portlet, 0);
	} else {
	    portal.add(portlet, 1);
	}
    }
}
